<?php

namespace Kateba\GOOBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Kateba\GOOBundle\Form\Search;
use Kateba\GOOBundle\Entity\Call;
use Kateba\GOOBundle\Entity\Project;

/**
 * Search controller.
 *
 */
class SearchController extends Controller {

    /**
     * the search page
     * @author Mahmoud
     */
    public function indexAction() {
        //prepare the search form
        $form = $this->createForm(new Search());
        return $this->render('KatebaGOOBundle:Search:search.html.twig', array('form' => $form->createView()));
    }

    /**
     * this is the ajax search action
     * @author Mahmoud
     */
    public function searchResultsAction() {
        $request = $this->getRequest();
        $form = $this->createForm(new Search());
        $form->bindRequest($request);
        $data = $form->getData();
        $type = $request->get('type');
        $projectType = 0;
        $mobilityLevel = 0;
        $fundingAgency = 0;
        $studyField = 0;
        $scope = 0;
        $country = 0;
        $page = 1;
        //the results per page number
        $itemsPerPage = 9;
        if (!empty($data['projectType']) && ($type == 'project' || $type == '0')) {
            $projectType = $data['projectType'];
        }
        if (!empty($data['mobilityLevel']) && ($type == 'call' || $type == '0')) {
            $mobilityLevel = $data['mobilityLevel']->getId();
        }
        if (!empty($data['fundingAgency'])) {
            $fundingAgency = $data['fundingAgency']->getId();
        }
        if (!empty($data['studyField'])) {
            $studyField = $data['studyField']->getId();
        }
        if (!empty($data['scope']) && ($type == 'project' || $type == '0')) {
            $scope = $data['scope'];
        }
        if (!empty($data['country']) && ($type == 'call' || $type == '0')) {
            $country = $data['country']->getId();
        }
        if (!empty($data['page'])) {
            $page = $data['page'];
        }
        $results = $this->getDoctrine()->getEntityManager()->getRepository('KatebaGOOBundle:Search')->search($type, $projectType, $mobilityLevel, $fundingAgency, $studyField, $country, $scope, $page, $itemsPerPage);
        $entities = $results['entities'];
        $count = $results['count'];
        //calculate the last page number
        $lastPageNumber = (int) ($count / $itemsPerPage);
        if (($count % $itemsPerPage) > 0) {
            $lastPageNumber++;
        }
        foreach ($entities as &$entity) {
            if ($entity['type'] == 'call') {
                $call = new Call();
                $call->setImage($entity['image']);
                $entity['image'] = $call->getWebPath();
            }
            if ($entity['type'] == 'project') {
                $project = new Project();
                $project->setImage($entity['image']);
                $entity['image'] = $project->getWebPath();
            }
        }
        return $this->render('KatebaGOOBundle:Search:searchResults.html.twig', array(
                    'entities' => $entities,
                    'count' => $count,
                    'page' => $page,
                    'lastPageNumber' => $lastPageNumber
                ));
    }

}
